/*     */ package com.zimbra.cs.filter;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.filter.jsieve.ActionFlag;
/*     */ import com.zimbra.cs.mailbox.DeliveryContext;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.SpamHandler;
/*     */ import com.zimbra.cs.service.util.SpamHandler.SpamReport;
/*     */ import java.io.IOException;
/*     */ import java.util.Collection;
/*     */ import java.util.List;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class IncomingMessageHandler
/*     */   implements FilterHandler
/*     */ {
/*     */   private OperationContext octxt;
/*     */   private DeliveryContext dctxt;
/*     */   private ParsedMessage parsedMessage;
/*     */   private Mailbox mailbox;
/*     */   private int defaultFolderId;
/*     */   private String recipientAddress;
/*     */   private int size;
/*     */   private boolean noICal;
/*     */   
/*     */   public IncomingMessageHandler(OperationContext octxt, DeliveryContext dctxt, Mailbox mbox, String recipientAddress, ParsedMessage pm, int size, int defaultFolderId, boolean noICal)
/*     */   {
/*  60 */     this.octxt = octxt;
/*  61 */     this.dctxt = dctxt;
/*  62 */     this.mailbox = mbox;
/*  63 */     this.recipientAddress = recipientAddress;
/*  64 */     this.parsedMessage = pm;
/*  65 */     this.size = size;
/*  66 */     this.defaultFolderId = defaultFolderId;
/*  67 */     this.noICal = noICal;
/*     */   }
/*     */   
/*     */   public Message getMessage()
/*     */   {
/*  72 */     return null;
/*     */   }
/*     */   
/*     */   public MimeMessage getMimeMessage()
/*     */   {
/*  77 */     return this.parsedMessage.getMimeMessage();
/*     */   }
/*     */   
/*     */   public ParsedMessage getParsedMessage()
/*     */   {
/*  82 */     return this.parsedMessage;
/*     */   }
/*     */   
/*     */   public String getDefaultFolderPath() throws ServiceException
/*     */   {
/*  87 */     return this.mailbox.getFolderById(this.octxt, this.defaultFolderId).getPath();
/*     */   }
/*     */   
/*     */   public Message explicitKeep(Collection<ActionFlag> flagActions, String[] tags)
/*     */     throws ServiceException
/*     */   {
/*  93 */     return addMessage(this.defaultFolderId, flagActions, tags);
/*     */   }
/*     */   
/*     */   public ItemId fileInto(String folderPath, Collection<ActionFlag> flagActions, String[] tags)
/*     */     throws ServiceException
/*     */   {
/*  99 */     ItemId id = FilterUtil.addMessage(this.dctxt, this.mailbox, this.parsedMessage, this.recipientAddress, folderPath, false, FilterUtil.getFlagBitmask(flagActions, Flag.BITMASK_UNREAD), tags, -1, this.octxt);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     try
/*     */     {
/* 106 */       Folder folder = this.mailbox.getFolderByPath(this.octxt, folderPath);
/* 107 */       if ((folder.getId() == 4) && (id.isLocal())) {
/* 108 */         SpamHandler.SpamReport report = new SpamHandler.SpamReport(true, "filter", folderPath);
/* 109 */         SpamHandler.getInstance().handle(this.octxt, this.mailbox, id.getId(), MailItem.Type.MESSAGE, report);
/*     */       }
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/* 112 */       ZimbraLog.filter.debug("Unable to do spam training for message %s because folder path %s does not exist.", new Object[] { id, folderPath });
/*     */     }
/*     */     catch (ServiceException e) {
/* 115 */       ZimbraLog.filter.warn("Unable to do spam training for message %s.", id, e);
/*     */     }
/*     */     
/* 118 */     return id;
/*     */   }
/*     */   
/*     */   public Message implicitKeep(Collection<ActionFlag> flagActions, String[] tags) throws ServiceException
/*     */   {
/* 123 */     int folderId = (this.mailbox.getAccount().isFeatureAntispamEnabled()) && (SpamHandler.isSpam(getMimeMessage())) ? 4 : this.defaultFolderId;
/*     */     
/* 125 */     return addMessage(folderId, flagActions, tags);
/*     */   }
/*     */   
/*     */   private Message addMessage(int folderId, Collection<ActionFlag> flagActions, String[] tags) throws ServiceException
/*     */   {
/*     */     try {
/* 131 */       DeliveryOptions dopt = new DeliveryOptions().setFolderId(folderId).setNoICal(this.noICal).setRecipientEmail(this.recipientAddress);
/* 132 */       dopt.setFlags(FilterUtil.getFlagBitmask(flagActions, Flag.BITMASK_UNREAD)).setTags(tags);
/* 133 */       return this.mailbox.addMessage(this.octxt, this.parsedMessage, dopt, this.dctxt);
/*     */     } catch (IOException e) {
/* 135 */       throw ServiceException.FAILURE("Unable to add incoming message", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void redirect(String destinationAddress)
/*     */     throws ServiceException
/*     */   {
/* 142 */     FilterUtil.redirect(this.octxt, this.mailbox, this.parsedMessage.getOriginalMessage(), destinationAddress);
/*     */   }
/*     */   
/*     */   public void reply(String bodyTemplate) throws ServiceException, MessagingException
/*     */   {
/* 147 */     FilterUtil.reply(this.octxt, this.mailbox, this.parsedMessage, bodyTemplate);
/*     */   }
/*     */   
/*     */ 
/*     */   public void notify(String emailAddr, String subjectTemplate, String bodyTemplate, int maxBodyBytes, List<String> origHeaders)
/*     */     throws ServiceException, MessagingException
/*     */   {
/* 154 */     FilterUtil.notify(this.octxt, this.mailbox, this.parsedMessage, emailAddr, subjectTemplate, bodyTemplate, maxBodyBytes, origHeaders);
/*     */   }
/*     */   
/*     */ 
/*     */   public int getMessageSize()
/*     */   {
/* 160 */     return this.size;
/*     */   }
/*     */   
/*     */   public void discard() {}
/*     */   
/*     */   public void beforeFiltering() {}
/*     */   
/*     */   public void afterFiltering() {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/IncomingMessageHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */